  var TBL_COLS = 3;
  
  var NS_GPHOTO = "http://schemas.google.com/photos/2007";
  var NS_MEDIA = "http://search.yahoo.com/mrss/";
  
  function createTable(entries) {
    var table = document.createElement("table");
    var cols = TBL_COLS;
    var rows = Math.ceil(entries.length/cols);
    
    for(var i = 0; i < rows; ++i) {
      var tr = document.createElement("tr");
      for(var j=0; j < cols; ++j) {
        var k = i*cols + j;
        if(k < entries.length) {
          var entry = entries[k];
          var td = document.createElement("td");
          td.appendChild(getEntryDiv(entry));
          tr.appendChild(td);
        }
      }
      table.appendChild(tr);
    }
    return table;
  }
  
  function getEntryDiv(entry) {
    var div = document.createElement("div");
    div.setAttribute("class", "cell");
    
    div.appendChild(getPhoto(entry));
    
    var titleDiv = document.createElement("div");
    titleDiv.setAttribute("class", "title");
    
    var a = document.createElement("a");
    a.setAttribute("class", "title");
    a.setAttribute("href", entry.link);
    a.appendChild(document.createTextNode(entry.title));
    titleDiv.appendChild(a);
    
    var numphotosSpan = document.createElement("span");
    numphotosSpan.setAttribute("class", "info");
    numphotosSpan.appendChild(document.createTextNode(" ("+getNumPhotos(entry)+")"));
    titleDiv.appendChild(numphotosSpan);
    
    div.appendChild(titleDiv);
    
    var dateDiv = document.createElement("div");
    dateDiv.setAttribute("class", "title");
    var dateSpan = document.createElement("span");
    dateSpan.setAttribute("class", "info");
    dateSpan.appendChild(document.createTextNode(formatDate(entry.publishedDate)));
    dateDiv.appendChild(dateSpan);
    div.appendChild(dateDiv);
    
    return div;
  }
  
  function getElement(entry, ns, name) {
    return google.feeds.getElementsByTagNameNS(entry.xmlNode, ns, name)[0];
  }
  
  function getValueForElement(entry, ns, name) {
    var element = getElement(entry, ns, name);
    return element.firstChild.nodeValue;
  }
  
  function getGPhotoValue(entry, name) {
    return getValueForElement(entry, NS_GPHOTO, name);
  }
  
  function getNumPhotos(entry) {
    return getGPhotoValue(entry, "numphotos");
  }
  
  function getPhotoURL(entry) {
    var mediaContentElem = getElement(entry, NS_MEDIA, "thumbnail");
    var url = mediaContentElem.getAttribute("url");
    var cachedUrl = gadgets.io.getProxyUrl(url);
    return cachedUrl;
  }
  
  function getPhoto(entry) {
    var div = document.createElement("div");
    div.setAttribute("class", "img");
    
    var a = document.createElement("a");
    a.setAttribute("href", entry.link);
    a.setAttribute("class", "img");
    
    div.appendChild(a);
    
    var img = document.createElement("img");
    img.setAttribute("src", getPhotoURL(entry));
    img.setAttribute("class", "img");
    img.setAttribute("height", "160");
    img.setAttribute("width", "160");
    
    a.appendChild(img);
    
    return div;
  }
  
  function formatDate(dateString) {
    var arr = dateString.split(" ");
    var day = arr[1];
    if(day.charAt(0) == '0') {
      day = day.substring(1);
    }
    return ""+day+" "+arr[2]+", "+arr[3];
  }
